<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb20" :style="{'visibility': currentType !== '5'? '' : 'hidden'}">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row :gutter="10" class="mb20">
      <el-radio-group v-model="currentType" @change="handleTypeChange">
        <el-radio-button label="1">个人纸质合同</el-radio-button>
        <el-radio-button label="2">企业纸质合同</el-radio-button>
        <el-radio-button label="3">房屋委托合同</el-radio-button>
        <el-radio-button label="4">服务合同</el-radio-button>
        <el-radio-button label="5">电子合同</el-radio-button>
      </el-radio-group>
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      :style="{'visibility': currentType !== '5'? '' : 'hidden'}"
    >
      <el-form-item label="名称" prop="contractTemplateName">
        <el-input
          v-model="queryParams.contractTemplateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['enterprise:contractTemplate:query']"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      ref="table"
      v-if="currentType === '1' || currentType === '2' || currentType === '3' || currentType === '4'"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="名称" align="center" prop="contractTemplateName"/>
      <el-table-column label="状态" align="center" prop="templateState">
        <template #default="scope">
          <span>{{ templateStateListText(scope.row.templateState) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['enterprise:contractTemplate:edit']"
          >编辑内容
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleSetting(scope.row)"
            v-hasPermi="['enterprise:contractTemplate:edit']"
          >设置
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >预览合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['enterprise:contractTemplate:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-table :data="electronicContractList" v-if="currentType === '5'">
      <el-table-column label="名称" align="center" prop="signTemplateName"/>

      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览合同
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增 -->
    <Add v-model="open" :title="title" @success="handleQuery"/>
    <!-- 修改 -->
    <Edit v-model="editVisible" :title="title" :formData="form" @success="handleQuery"/>
    <!-- 设置 -->
    <Setting v-model="settingVisible" :title="title" :formData="form" @success="handleQuery"/>
    <!-- 预览 -->
    <Preview v-model="previewVisible" :title="title" :formData="form"/>
    <!-- 详情 -->
    <Detail v-model="detailVisible" :title="title" :formData="form"/>
  </div>
</template>

<script>

import {
  listContractTemplate,
  delContractTemplate,
  changeUsageStatus,
  getContractTemplate,
  pageContractTemplate,
  previewContractTemplate,
  getContractTemplateWithPlaceholder,
  pageElectronicVisa, previewElectronicContract
} from '@/api/contract/template'
import Add from './components/Add.vue'
import Edit from './components/Edit.vue'
import Detail from './components/Detail.vue'
import Setting from './components/Setting.vue'
import Preview from './components/Preview.vue'

export default {
  name: 'Template',
  dicts: [],
  components: {
    Add,
    Edit,
    Setting,
    Preview,
    Detail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 电子合同列表
      electronicContractList: [],
      //模板详情数据
      unsubscribeRuleData: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 编辑弹窗
      editVisible: false,
      // 设置弹窗
      settingVisible: false,
      // 预览弹窗
      previewVisible: false,
      //详情弹窗
      detailVisible: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractTemplateName: '',
        templateState: '',//启用状态:0.显示;1.隐藏
        templateType: '1'//模板类型:1.个人纸质合同;2.企业纸质合同;3.房屋委托合同;4.服务合同;
      },
      //状态列表
      templateStateList: [
        { label: '显示', value: '0' },
        { label: '隐藏', value: '1' }
      ],
      // 表单参数
      form: {},
      // 当前合同类型
      currentType: '1'
    }
  },
  computed: {
    templateStateListText() {
      return (value) => {
        const obj = this.templateStateList.find(item => item.value === value)
        return obj ? obj.label : ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getList()
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      pageContractTemplate(this.addDateRange(this.queryParams, this.dateRange))
        .then((response) => {
          this.list = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    /** 查询电子合同列表 */
    getElectronicContractList() {
      this.loading = true
      pageElectronicVisa(this.addDateRange(this.queryParams, this.dateRange))
        .then((response) => {
          this.electronicContractList = response.data.signTemplates
          this.total = response.data.totalCount
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        templateName: '', //合同模板名称
        content: '', //合同模板内容
        apartmentType: undefined //公寓类型(出租周期)：1短租公寓;2长租公寓
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.currentType == 5) {
        this.getElectronicContractList()
      } else {
        this.queryParams.pageNum = 1
        this.getList()
      }

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (this.currentType == 5) {
        this.$message.error('电子合同不能新增')
        return
      }
      this.reset()
      this.open = true
      this.title = '新增模板'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const contractTemplateId = row.contractTemplateId || this.ids
      getContractTemplateWithPlaceholder(contractTemplateId).then((response) => {
        this.form = response.data
        this.editVisible = true
        this.title = '编辑合同内容'
      })
    },
    /** 设置按钮操作 */
    handleSetting(row) {
      this.form = { ...row }
      this.settingVisible = true
      this.title = '设置'
    },
    /** 预览按钮操作 */
    handleView(row) {
      console.log('row', row)
      previewContractTemplate(row.contractTemplateId).then((response) => {
        this.form = response.data
        this.title = '预览合同'
        this.previewVisible = true
      })
    },
    /** 预览电子合同按钮操作 */
    handlePreview(row) {
      previewElectronicContract(row.signTemplateId).then((response) => {
        this.form = response.data
        this.title = '预览合同'
        // this.previewVisible = true
        window.open(response.data)
      })
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset()
      const contractTemplateId = row.contractTemplateId || this.ids
      getContractTemplate(contractTemplateId).then((response) => {
        this.form = response.data
        this.detailVisible = true
        this.title = '详情'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.contractTemplateId || this.ids
      this.$modal
        .confirm('确定删除数据？')
        .then(function() {
          return delContractTemplate(templateIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    // 模板状态修改
    handleStatusChange(row) {
      console.log('usageStatus', row.usageStatus)
      let text = row.usageStatus === '1' ? '启用' : '停用'
      this.$modal
        .confirm('确认要"' + text + '""' + row.templateName + '"合同模板吗？')
        .then(function() {
          return changeUsageStatus(row.contractTemplateId, row.usageStatus)
        })
        .then(() => {
          this.$modal.msgSuccess(text + '成功')
        })
        .catch(function() {
          console.log('err')
          row.usageStatus = row.usageStatus === '1' ? '0' : '1'
        })
    },
    // 合同类型切换
    handleTypeChange(val) {
      if (val == 5) {
        this.handleQuery()
      } else {
        this.queryParams.contractTemplateName = ''
        this.queryParams.templateType = val
        this.handleQuery()
      }

    }

  }

}
</script>
<style lang="scss" scoped>
.info {
  padding: 10px 15px;
  margin: 15px 0;
  background: #e6f7ff;
  color: #1890ff;

  span {
    color: #565c5f;
  }

  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}

.detail-item {
  margin-bottom: 20px;

  .label {
    color: #7f7f7f;
    margin-bottom: 10px;
  }

  .value {
    font-size: 16px;
  }
}
</style>
